//
//  FFRedPacketsRecordCell.m
//  LiMaoRedPackets
//
//  Created by JayLuo on 2020/8/22.
//

#import "FFRedPacketsRecordCell.h"
#import "Masonry.h"
#import "FFRedPacketsRecordModel.h"

@interface FFRedPacketsRecordCell()
@property(nonatomic, weak) UIImageView *avatarImageView;
@property(nonatomic, weak) UILabel *nameLabel;
@property(nonatomic, weak) UILabel *timeLabel;
@property(nonatomic, weak) UILabel *moneyLabel;
@property(nonatomic, weak) UIButton *walletButton;
@end

@implementation FFRedPacketsRecordCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)refreshWithModel:(id)cellModel type:(LIMRedPacketsRecordType)type {
    if (type == LIMRedPacketsRecordList) {
        FFRedPacketsRecord *model = cellModel;
        _nameLabel.text = model.name;
        _moneyLabel.text = [NSString stringWithFormat:LLang(@"%.2f元"), (double)model.open_amount/100.0];
        _walletButton.hidden = !model.is_luck;
        _timeLabel.text = [self UTCchangeDate:[NSString stringWithFormat:@"%ld", model.open_time]];
        NSString *avatarString = [WKAvatarUtil getAvatar:model.uid];
        [_avatarImageView setImageWithURL:[NSURL URLWithString:avatarString] placeholderImage:[WKApp shared].config.defaultAvatar];
    }else if(type == LIMRedPacketsRecordInList) {
        FFRedPacketsRecord *model = cellModel;
        _nameLabel.text = model.sender_name;
        _moneyLabel.text = [NSString stringWithFormat:LLang(@"%.2f元"), (double)model.open_amount/100.0];
        _walletButton.hidden = !model.is_luck;;
        _timeLabel.text =  model.open_date;
        NSString *avatarString = [WKAvatarUtil getAvatar:model.sender_uid];
        [_avatarImageView setImageWithURL:[NSURL URLWithString:avatarString] placeholderImage:[WKApp shared].config.defaultAvatar];
    }else if(type == LIMRedPacketsRecordOutList) {
        FFRedPacketsRecord *model = cellModel;
        _nameLabel.text = [WKApp shared].loginInfo.extra[@"name"];
        _moneyLabel.text = [NSString stringWithFormat:LLang(@"%.2f元"), (double)model.amount/100.0];
        _walletButton.hidden = !model.is_luck;;
        _timeLabel.text =  model.create_date;
        NSString *avatarString = [WKAvatarUtil getAvatar:[WKApp shared].loginInfo.uid];
        [_avatarImageView setImageWithURL:[NSURL URLWithString:avatarString] placeholderImage:[WKApp shared].config.defaultAvatar];
    }
}

- (NSString *)UTCchangeDate:(NSString *)utc {
    
    NSTimeInterval time = [utc doubleValue];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"YYYY.MM.dd HH:mm"];
    
    NSString *staartstr = [dateformatter stringFromDate:date];
    
    return staartstr;
    
}

+ (NSString *)cellId {
    return @"FFRedPacketsRecordCell";
}

- (void)setupUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImage *avatarImage = [self imageName:@"avatar_male"];
    UIImageView *avatarImageView = [[UIImageView alloc] initWithImage:avatarImage];
    [self.contentView addSubview:avatarImageView];
    avatarImageView.layer.cornerRadius = 25;
    avatarImageView.layer.masksToBounds = YES;
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(15);
        make.left.equalTo(self.contentView.mas_left).mas_offset(15);
        make.width.height.equalTo(@50);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-15);
    }];
    
    _avatarImageView = avatarImageView;
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = @"昵称起名中的红包";
    nameLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    [self.contentView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(avatarImageView.mas_top);
        make.left.equalTo(avatarImageView.mas_right).mas_offset(10);
    }];
    
    _nameLabel = nameLabel;
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.text = @"20:29";
    timeLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0];
    timeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    timeLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(avatarImageView.mas_bottom);
        make.left.equalTo(nameLabel.mas_left);
    }];
    _timeLabel = timeLabel;
    
    UILabel *moneyLabel = [[UILabel alloc] init];
    moneyLabel.text = @"500.00元";
    moneyLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    moneyLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:moneyLabel];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(nameLabel.mas_bottom);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
    }];
    
    _moneyLabel = moneyLabel;
    
    UIButton *walletButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [walletButton setTitle:LLang(@"手气最佳") forState:(UIControlStateNormal)];
    walletButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    [walletButton setTitleColor:[UIColor colorWithRed:241/255.0 green:184/255.0 blue:9/255.0 alpha:1.0] forState:(UIControlStateNormal)];
    [walletButton setImage:[self imageName:@"luckyBoy"] forState:UIControlStateNormal];
    [self.contentView addSubview:walletButton];
    [walletButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(timeLabel.mas_bottom);
        make.right.equalTo(moneyLabel.mas_right);
    }];
    
    _walletButton = walletButton;
}

- (UIImage*)imageName:(NSString*)name {
    return [[WKApp shared] loadImage:name moduleID:@"LiMaoRedPackets"];
}
@end
